//
//  ViewController.m
//  Music
//
//  Created by mac on 16-5-26.
//  Copyright (c) 2016年 zhiyou. All rights reserved.
//

#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController (){
    NSTimer *_timer;
}

@property(nonatomic,strong)AVAudioPlayer *player;
@property (weak, nonatomic) IBOutlet UISlider *slider;



@end

@implementation ViewController

- (void)creatPlayer:(NSString *)url{
 //ARC下可写可不写
    if (self.player) {
        self.player = nil;
    }
    
    NSURL *fileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:url ofType:nil]];
    
    self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:fileUrl error:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
     [self creatPlayer:@"雨一直下.mp3"];
    
    
    

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self playSound:@"paipai.caf"];
}


void soundCompleteCallback(SystemSoundID soundID,void * clientData){
   
    NSLog(@"播放完成...");
}

/*
 使用System Sound Service 播放音效的步骤如下：
 
1. 调用AudioServicesCreateSystemSoundID(   CFURLRef  inFileURL, SystemSoundID*   outSystemSoundID)函数获得系统声音ID。
 2.如果需要监听播放完成操作，则使用AudioServicesAddSystemSoundCompletion(  SystemSoundID inSystemSoundID,
 CFRunLoopRef  inRunLoop, CFStringRef  inRunLoopMode, AudioServicesSystemSoundCompletionProc  inCompletionRoutine, void*  inClientData)方法注册回调函数。
 3.调用AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID) 或者AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID) 方法播放音效（后者带有震动效果）。
 

 */

- (void)playSound:(NSString *)name{

    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
    NSURL *url = [NSURL fileURLWithPath:path];
    //1.获得系统声音ID
    SystemSoundID soundID=0;
    /**
     * inFileUrl:音频文件url
     * outSystemSoundID:声音id（此函数会将音效文件加入到系统音频服务中并返回一个长整形ID）
     */
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
    //如果需要在播放完之后执行某些操作，可以调用如下方法注册一个播放完成回调函数
    AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL);
    //2.播放音频
    AudioServicesPlaySystemSound(soundID);//播放音效
    //    AudioServicesPlayAlertSound(soundID);//播放音效并震动
    
}

- (void)playMusic:(NSString *)name{

}
- (IBAction)play:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
       
        [self.player play];
        [self startTimer];
       

    }else{
        [self.player pause];
    }
}

- (IBAction)last:(UIButton *)sender {
     [self creatPlayer:@"雨一直下.mp3"];
}

- (IBAction)next:(UIButton *)sender {
     [self creatPlayer:@"雨一直下.mp3"];
}

- (void)viewDidDisappear:(BOOL)animated{
    [self stopTimer];
}

- (void)startTimer{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    }
    

}

- (void)stopTimer{

    if (!_timer) {
        [_timer invalidate];
        _timer =nil;
    }
    
}

- (void)onTimer{
    float pro = self.player.currentTime/self.player.duration;
    self.slider.value = pro;
    

}
- (IBAction)sliderChange:(id)sender {
    self.player.currentTime = self.player.duration * self.slider.value;
}

@end






